package com.siruier.boss.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import com.siruier.boss.ui.R
import com.siruier.boss.ui.ext.dp
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.listener.OnValidationResultListener
import com.siruier.boss.ui.utils.VariableViewUtils

class CommonButton : AppCompatTextView, View.OnClickListener, OnValidationResultListener {
    companion object {
        const val CB_NORMAL = 1 // 直接使用主色调非空心
        const val CB_STROKE_TEXT = 2 // 字体色色空心
        const val CB_RED = 3 // 红色字体色色非空心
        const val CB_STROKE_NORMAL = 4 // 正常边框
        const val CB_STROKE_HINT = 5 // 浅色边框
        const val CB_AUTH = 6 // 认证
        const val CB_BLUE = 7 // 蓝色调实心
        const val CB_WELFARE = 8 // 福利
        const val CB_MALL_GRADUAL = 9; // 商城橙色调实心渐变
        const val CB_MALL_STROKE = 10; // 商城橙色空心边线
        const val CB_HINT_TEXT_STROKE = 11; // 主深灰色空心边线
        const val CB_MALL = 12; // 商城橙色调实心
        const val CB_MAIN_TEXT_STROKE = 13; // 主字体色色空心边线
        const val CB_STROKE_WHITE = 14; // 白色空心边线
    }

    private var mOnClickListener: OnClickListener? = null
    private var defaultEnable = true// 是否活跃
    private var mVariableViewUtils: VariableViewUtils? = null
    private var normalDrawable: Drawable? = null
    private var enableFalseDrawable: Drawable? = null
    private var mCBStyle = CB_NORMAL
    var stroke = 1.dp
    var textSize = 16.dp
    var radius = 30.dp.toFloat()

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonButton)
            mCBStyle = typedArray.getInt(R.styleable.CommonButton_cb_style, CB_NORMAL)
            textSize = typedArray.getDimensionPixelSize(R.styleable.CommonButton_but_text_size, textSize)
            radius = typedArray.getDimension(R.styleable.CommonButton_radius, radius)
            typedArray.recycle()
        }
        changeStyle()
        enableFalseDrawable = GradientDrawable().apply {
            setColor(R.color.text_hint_color.toColor())
            cornerRadius = radius
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        gravity = Gravity.CENTER
    }

    fun changeStyle(cbStyle: Int = mCBStyle) {
        mCBStyle = cbStyle
        when (cbStyle) {
            CB_NORMAL -> {
                normalDrawable = context.getDrawable(R.drawable.bg_main_button_gradual)
                setTextColor(R.color.white.toColor())
            }
            CB_RED -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.button_red.toColor());
                    cornerRadius = radius
                }
                setTextColor(R.color.white.toColor())
            }
            CB_STROKE_TEXT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_color.toColor())
                }
                setTextColor(R.color.text_color.toColor())
            }
            CB_STROKE_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.main_color.toColor())
                }
                setTextColor(R.color.main_color.toColor())
            }
            CB_STROKE_HINT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_login_hint.toColor())
                }
                setTextColor(R.color.text_login_hint.toColor())
            }
            CB_AUTH -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(R.color.text_tag.toColor());
                }
                setTextColor(R.color.text_hint_color_2.toColor())
            }
            CB_BLUE -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.blue.toColor())
                    cornerRadius = radius
                }
                setTextColor(R.color.white.toColor())
            }
            CB_WELFARE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setColor(R.color.text_color.toColor());
                }
                setTextColor(R.color.welfare_num.toColor())
            }
            CB_MALL_GRADUAL -> {
                normalDrawable = ContextCompat.getDrawable(context, R.drawable.buy_now_gradient_default)
                setTextColor(ContextCompat.getColor(context, R.color.white))
            }
            CB_MALL_STROKE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.mall_main.toColor())
                }
                setTextColor(R.color.mall_main.toColor())
            }
            CB_HINT_TEXT_STROKE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_hint_color_2.toColor())
                }
                setTextColor(R.color.text_hint_color_2.toColor())
            }
            CB_MALL -> {
                normalDrawable =
                    GradientDrawable().apply { setColor(R.color.mall_main.toColor());cornerRadius = radius }
                setTextColor(R.color.white.toColor())
            }
            CB_MAIN_TEXT_STROKE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_color.toColor())
                }
                setTextColor(R.color.text_color.toColor())
            }
            CB_STROKE_WHITE -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.white.toColor())
                }
                setTextColor(R.color.white.toColor())
            }
        }
        ViewCompat.setBackground(this, if (defaultEnable) normalDrawable else enableFalseDrawable)
    }

    fun getCbStyle() = mCBStyle

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    fun setVariableButtonUtils(variableViewUtils: VariableViewUtils) {
        mVariableViewUtils = variableViewUtils
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && defaultEnable) {
            //if (BLUtils.notRepeatClick()) {
            if (mVariableViewUtils == null) {
                mOnClickListener?.onClick(v)
            } else if (mVariableViewUtils!!.clickValidator()) {
                mOnClickListener?.onClick(v)
            }
            //}
        }
    }

    override fun enable(enable: Boolean): Boolean {
        if (enable && !defaultEnable) ViewCompat.setBackground(this, normalDrawable)
        if (!enable && defaultEnable) ViewCompat.setBackground(this, enableFalseDrawable)
        defaultEnable = enable
        return defaultEnable
    }
}